package com.ruoyi.message.manage.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.plus.web.page.PageDto;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.message.general.entity.MessageSendRecord;
import com.ruoyi.message.manage.model.dto.ManageMessageSendRecordDto;
import com.ruoyi.message.manage.model.vo.ManageMessageSendRecordVo;
import com.ruoyi.message.manage.service.ManageMessageSendRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/message/messageSendRecord")
@Api(tags = "消息发送记录")
public class ManageMessageSendRecordController extends BasePlusController {

    @Autowired
    private ManageMessageSendRecordService manageMessageSendRecordService;

    /**
     * 查询短信发送记录列表
     */
    @PreAuthorize("@ss.hasPermi('message:message_send_record:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页")
    public TableDataInfo<ManageMessageSendRecordVo.CommonVo> list(ManageMessageSendRecordDto.CommonDto commonDto) {
        PageDto pageDto = startPage();
        Page<MessageSendRecord> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        if (pageDto.getOrderItem() != null) {
            page.addOrder(pageDto.getOrderItem());
        }
        MessageSendRecord messageSendRecord = Convert.convert(MessageSendRecord.class, commonDto);
        LambdaQueryWrapper<MessageSendRecord> wrapper = Wrappers.lambdaQuery(messageSendRecord);
        page = manageMessageSendRecordService.getByPage(page, wrapper);
        return getDataTable(page);
    }

    /**
     * 导出消息发送记录列表
     */
    @PreAuthorize("@ss.hasPermi('message:message_send_record:export')")
    //@Log(title = "消息发送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void export(HttpServletResponse response, ManageMessageSendRecordDto.CommonDto commonDto) throws IOException {
        MessageSendRecord messageSendRecord = Convert.convert(MessageSendRecord.class, commonDto);
        List<MessageSendRecord> list = manageMessageSendRecordService.getByList(messageSendRecord);
        ExcelUtil<MessageSendRecord> util = new ExcelUtil<MessageSendRecord>(MessageSendRecord.class);
        util.exportExcel(response, list, "messageSendRecord");
    }

    /**
     * 获取消息发送记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:message_send_record:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查看")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        MessageSendRecord messageSendRecord = manageMessageSendRecordService.getById(id);
        ManageMessageSendRecordVo.CommonVo commonVo = Convert.convert(ManageMessageSendRecordVo.CommonVo.class, messageSendRecord);
        return AjaxResult.success(commonVo);
    }

}
